#ifndef _DXYTHREADPOOL_H_
#define _DXYTHREADPOOL_H_

#include <functional>
#include <thread>
#include <future>
#include <vector>
#include <condition_variable>
#include <iostream>
#include <memory>

#include "DxyLockList.h"



namespace dxy::threadPool
{

class DxyThreadPool
{
public:
    DxyThreadPool();
    ~DxyThreadPool();

    void start(int num);
    void stop();

    template<class T, class... Args>
    auto addTask(T & f, Args&&... args);

private:
    void work();

private:
    int                                         m_threadNum         = 8;
    bool                                        m_isStop            = false;
    DxyLockList<std::function<void (void)>>     m_tasks;
    std::vector<std::unique_ptr<std::thread>>   m_threads;
    std::mutex                                  m_mtx;
    std::condition_variable                     m_cond;
};

template<class T, class... Args>
auto DxyThreadPool::addTask(T & f, Args&&... args)
{
    auto task = std::make_shared<std::packaged_task<decltype(f(args...))()>>(std::bind(f, std::forward<Args>(args)...));
    m_tasks.push_back([task] () {(*task)();});
    m_cond.notify_one();
    return task->get_future();
}


}

#endif